<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-waves type="primary" style="float:right; margin-left:10px" @click="handleUserInit">
        初始化
      </el-button>
      <el-button v-if="listQuery.user_type == 1" v-waves type="primary" style="float:right" @click="handleAddStudent">
        调入学员
      </el-button>
      <el-button v-if="listQuery.user_type == 2" v-waves type="primary" style="float:right" @click="handleAddOther">
        调入教职工
      </el-button>
      <el-button v-if="listQuery.user_type == 3" v-waves type="primary" style="float:right" @click="handleAddOther">
        调入其他
      </el-button>
      <el-radio-group v-model="listQuery.user_type" class="filter-item" style="margin-right:10px" @change="getList">
        <el-radio-button v-for="(value,key) in usertypeOptions" :key="key" :label="key" :value="key">{{ value }}</el-radio-button>
      </el-radio-group>
      <el-select v-model="listQuery.type" placeholder="班级类型" clearable class="filter-item" style="width: 130px">
        <el-option v-for="(value,key) in typeOptions" :key="key" :label="value" :value="key" />
      </el-select>
      <el-input v-model="listQuery.userName" placeholder="用户姓名" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :header-cell-style="{background:'#ccc',color:'black'}"
      :data="list"
      border
      stripe
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="用户姓名" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班级名称" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.classInfo.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学期" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ semesterOptions[row.classInfo.semester] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份类型" align="center">
        <template slot-scope="{row}">
          <span>{{ usertypeOptions[row.user_type] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班级类型" align="center">
        <template slot-scope="{row}">
          <span>{{ typeOptions[row.type] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="组别" align="center">
        <template slot-scope="{row}">
          <span>{{ row.group }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button :type="row.type==0||row.type==1?'success':row.type==2?'primary':'danger'" size="mini" @click="handleUserOut(row,$index)">
            {{ row.type==1?'调出':(stuTypeOptions[row.type]) }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog title="调入学员" :visible.sync="showStu">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="110px" style="width: 400px; margin-left:50px;">
        <el-form-item label="学员" prop="user_id">
          <el-cascader
            v-model="temp.user_id"
            :options="classUserListOptions"
            :props="props"
            clearable
            filterable
          />
        </el-form-item>
        <el-form-item label="班级" prop="class_id">
          <el-select v-model="temp.class_id" class="filter-item">
            <el-option v-for="item in courseTimetableClassInfo" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="学期" prop="semester">
          <el-select v-model="temp.semester" class="filter-item">
            <el-option v-for="(value,key) in semesterOptions" :key="key" :label="value" :value="key" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showStu = false">
          取消
        </el-button>
        <el-button type="primary" @click="addStudentData()">
          确定
        </el-button>
      </div>
    </el-dialog>

    <el-dialog :title="textMap[listQuery.type]" :visible.sync="dialogFormVisible">
      <el-form ref="dataFormOther" :rules="rules" :model="temp" label-position="right" label-width="110px" style="width: 400px; margin-left:50px;">
        <el-form-item label="用户" prop="user_id">
          <SearchOptionUser v-model="temp.user_id" :type="usertypeToType[listQuery.type]" @setSearchUserId="searchUserId" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="addOtherData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { schoolUserList, timeTableUserPage, timetableUsreInit, timetableUsreOut, timetableUsreIn } from '@/api/course'
import waves from '@/directive/waves' // waves directive
import { notifySuccess, my_confirm, myConfirmMessage } from '@/utils/tool'
import { isInteger } from '@/utils/validate'
import Pagination from '@/components/Pagination'
import SearchOptionUser from '@/components/SearchOption/user'

export default {
  name: 'UserSet',
  components: { Pagination, SearchOptionUser },
  directives: { waves },
  props: ['userSettingData'],
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        type: undefined,
        userName: undefined,
        course_timetable_id: this.$route.query.id,
        user_type: '1',
        group: undefined,
        seat: undefined
      },
      textMap: {
        2: '添加班级主管',
        3: '添加其他用户'
      },
      buttonType: { 0: 'scucess', 2: 'primary', 3: 'danger' },
      stuTypeOptions: { 0: '调出', 2: '调回本班', 3: '移除' },
      semesterOptions: { 1: '第一学期', 2: '第二学期', 3: '第三学期', 4: '第四学期' },
      usertypeOptions: { 1: '学生', 2: '班级主管', 3: '其他' },
      usertypeToType: { 2: '1', 3: '3' }, // course_timetable_user的user_type抓换成user表的type
      typeOptions: { 0: '本班', 2: '调出', 3: '调入' },
      temp: {
        course_timetable_id: this.$route.query.id,
        user_type: '',
        user_id: '',
        class_id: '',
        semester: ''
      },
      dialogFormVisible: false,
      showStu: false,
      courseTimetableClassInfo: [],
      classUserListOptions: [],
      selectStudentIds: [],
      rules: {
        user_id: [{ required: true, message: '用户ID必填且是数字', trigger: 'change' }, { validator: isInteger, trigger: 'blur' }],
        class_id: [{ required: true, message: '班级ID必填且是数字', trigger: 'change' }, { validator: isInteger, trigger: 'blur' }],
        semester: [{ required: true, message: '学期必填且是数字', trigger: 'change' }, { validator: isInteger, trigger: 'blur' }]
      },
      props: { children: 'child', label: 'name', value: 'user_id', emitPath: false }
    }
  },
  created() {
    this.getUserInDialog()
    this.getList()
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getList() {
      this.listLoading = true
      timeTableUserPage(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.page
        this.courseTimetableClassInfo = response.data.courseTimetableClassInfo
        this.listLoading = false
      })
    },
    handleUserInit() {
      myConfirmMessage('是否初始化当前课表学生信息?', this, () => {
        const params = {
          courseTimetableId: this.listQuery.course_timetable_id
        }
        timetableUsreInit(params).then(() => {
          this.getList()
          this.$message({
            type: 'success',
            message: '初始化成功'
          })
        })
      })
    },
    handleUserOut(row, index) {
      const params = {
        userId: row.user_id,
        courseTimetableId: this.listQuery.course_timetable_id
      }
      my_confirm(this, () => {
        timetableUsreOut(params).then(() => {
          this.list.splice(index, 1)
          this.dialogFormVisible = false
          this.getList()
          notifySuccess(this, '移除成功')
        })
      })
    },
    resetTemp() {
      this.temp = {
        course_timetable_id: this.$route.query.id,
        user_type: this.listQuery.user_type,
        user_id: '',
        class_id: 0,
        semester: 0
      }
    },
    handleAddStudent() {
      this.resetTemp()
      this.showStu = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    addStudentData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          timetableUsreIn(this.temp).then(() => {
            notifySuccess(this, '创建成功')
            this.getList()
          })
        }
      })
    },
    handleAddOther() {
      this.resetTemp()
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataFormOther'].clearValidate()
      })
    },
    addOtherData() {
      this.$refs['dataFormOther'].validate((valid) => {
        if (valid) {
          this.temp.type = 3
          timetableUsreIn(this.temp).then(() => {
            notifySuccess(this, '创建成功')
            this.dialogFormVisible = false
            this.getList()
          })
        }
      })
    },
    getUserInDialog() {
      schoolUserList().then(response => {
        this.classUserListOptions = response.data
      })
    },
    searchCourseId: function(data) {
      this.listQuery.id = data.courseId
    },
    searchUserId: function(data) {
      this.temp.user_id = data.userId
    }
  }
}
</script>

<style lang="scss" scoped>
.row_box{
  margin: 15px 0;
  display: flex;
  align-items: center;
  span{
    display: block;
    width: 80px;
    text-align: right;
    margin-right: 10px;
  }
}
</style>
